<?php
namespace app\mobile\model;

use think\Request;

/**
 * Class MemberOrder
 * @package app\mobile\model 会员订单表
 */
class MemberOrder extends BaseModel
{
    /*
     * 订单和订单附属表一对多
     */
    public function memberOrderAttached()
    {
        return $this->hasMany('MemberOrderAttached','order_id','id');
    }
    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    /*
     * 订单状态
     */
    public function getStatusTextAttr($value,$data)
    {
        $status = ['待付款','待发货','待收货','已收货','已完结','已取消','申请退款','退款审核成功','退款审核失败','退货中','退货成功','退货失败'];
        return $status[$data['status']];
    }
    /*
     * 订单处理方式
     */
    public function getActTextAttr($value,$data)
    {
        $status = [
            ['取消订单','付款'],
            '申请退款',
            '查看物流',
            ['退换货','立即评价'],
            '已完结',
            '已取消',
            '退款退货审批中',
            '退款审核成功',
            '退款审核失败',
            '买家退货中',
            '退款退货成功',
            '退款退货失败',
        ];
        return $status[$data['status']];
    }
    /*
     * 处理方式的跳转路径
     */
    public function getActUrlAttr($value,$data)
    {
        $status = [
            ["{:url('mobile/member_order/cancel')}","{:url('mobile/user_category/choose_pay_type')}"],
            "{:url('mobile/member_order/refund')}",
            "{:url('mobile/member_order/refund')}",
            ['申请退货','立即评价'],
            '已完结',
            '已取消',
            '退款退货审批中',
            '退款审核成功',
            '退款审核失败',
            '买家退货中',
            '退款退货成功',
            '退款退货失败',
        ];
        return $status[$data['status']];
    }
    
    public function getPicUrlAttr($value)
    {
        return $value?$value:'/static/mobile/img/empty_img.png';
    }
    /*
     * 各订单的总数量统计
     */
    public function statistics($shop_id,$content,$search_type)
    {
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>'','shop_id'=>$shop_id])
                            ->column('shop_order_number'))); //全部
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>0,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //待付款
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>1,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //待收货
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>6,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //退款中
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>3,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //待评价
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>2,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //已发货
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>4,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //已完成
        $count[] = count(array_unique($this
                            ->where($search_type,'like','%'.$content.'%')
                            ->where(['status'=>5,'shop_id'=>$shop_id])
                            ->column('shop_order_number')));   //已关闭
        return $count;
    }
    /*
     * 店铺订单和商品多对一
     */
    public function shopGoods()
    {
        return $this->belongsTo('ShopGoods','goods_id','id');
    }
    /*
     * 订单和售货店铺多对一(我的订单)
     */
    public function shop()
    {
        return $this->hasOne('Shop','member_id','shop_id');
    }
    /*
     * 订单和买家多对一
     */
    public function member()
    {
        return $this->belongsTo('Member','member_id','id')->field('id,nickname');
    }
}